package org.poliglot.ipa;

/**
 * User: cristi
 * Date: 01.05.2010
 * Time: 09:45:21
 */
public abstract class PhoneticTranslator
{
    String input;
    int pos;

    public String parse(String input)
    {
        StringBuilder sb = new StringBuilder();

        this.input = input;
        this.pos = 0;

        for ( pos = 0; pos < input.length(); pos++ )
        {
            char c = Character.toLowerCase( input.charAt( pos ) );
            processCharacter( c, sb );
        }

        return sb.toString();
    }

    protected abstract void processCharacter( char c, StringBuilder sb );


    protected boolean isBefore( int pos, char... chars )
    {
        if ( pos < input.length() - 1 )
        {
            char next = input.charAt( pos + 1 );

            for ( char c : chars )
            {
                if ( c == next )
                {
                    return true;
                }
            }
        }

        return false;
    }


    protected char next()
    {
        return ( pos < input.length() - 1 ? input.charAt( pos + 1 ) : null );
    }


    public boolean isBetweenVowels()
    {
        return pos > 0 && pos < input.length() - 1 &&
                isVowel( input.charAt( pos - 1 ) ) && isVowel( input.charAt( pos + 1 ) );
    }


    public static boolean isVowel( char c )
    {
        return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y';
    }

}
